<%@page import="study.itwill.team5.admin.AdminItem"%>
<%@page import="study.itwill.jsp.WebHandler"%>
<%@page import="study.itwill.team5.house.ImageItem"%>
<%@page import="study.itwill.team5.house.TelItem"%>
<%@page import="study.itwill.team5.house.RoomItem"%>
<%@page import="study.itwill.team5.house.HouseCity"%>
<%@page import="study.itwill.team5.house.HouseItem"%>
<%@page import="study.itwill.team5.house.House"%>
<%@page import="study.itwill.common.PatternChecker"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page session="true" %>
<%
   WebHandler web = new WebHandler(request, response, out);
   AdminItem loginInfo = (AdminItem)session.getAttribute("ADMIN_INFO");
   
   // 로그인 정보가 없을 경우 로그인 페이지로 이동
   if (loginInfo == null) {
      web.goPage("/Team-Project/login.jsp", "로그인이 필요합니다");
      return;
   }
%>
<%
	String seq = request.getParameter("seq");

	/** 파라미터의 유효성 검사 */
	if(!PatternChecker.isValue(seq)) {
		out.print("<script type='text/javascript'>alert('민박 일련번호 파라미터가 없습니다.');history.back();</script>");
		return;
	}
	
	if(!PatternChecker.isNum(seq)) {
		out.print("<script type='text/javascript'>alert('민박 일련번호 파라미터가 형식에 맞지 않습니다.');history.back();</script>");
		return;
	}
	
	int house_number = Integer.parseInt(seq);
%>
<%@ include file="../dbcon.jsp"%>
<%
	
	// 민박 데이터 조회
	House house = new House();
	HouseItem item = house.viewHouse(house_number);
	
	HouseCity city = new HouseCity();
	
	ArrayList<TelItem> t_item = house.telList(house_number);
	ArrayList<ImageItem> i_item = house.imageList(house_number);
	ArrayList<RoomItem> r_item = house.roomList(house_number);
	
	
	if (item == null) {
		out.print("<script type='text/javascript> alert('데이터조회에 실패했습니다. 잠시후 다시 시도해 주세요.'); history.back();</script>");
		return;
	}
%>
<!DOCTYPE html>
<html lang="ko">
  <head>
<%@ include file="../head.jsp"%>
  </head>
  <body>
<%@ include file="../menu.jsp"%>
    <!-- Javascript -->
    <script src="/italia2/assets/js/jquery.js"></script>
    <script src="/italia2/assets/js/bootstrap.min.js"></script>
    <script src="/italia2/assets/js/string.js"></script>
<%DBHandler.disconnect();%>
    <!-- 웹 페이지 영역 -->
    <div class="container">
	    <div class="page-header">
	    		<h1>
	    			민박 수정 - <small>민박을 수정하세요.</small>
	    		</h1>
	    	</div>
	    	
	    	<form id="frmHouseEdit" class="form-horizontal" role="form" method="post" action="editHouseOk.jsp">
	    		<fieldset>
	    		<input type="hidden" name="seq" value="<%=seq%>" />
	    		<input type="hidden" name="city_seq" value="<%=item.getHouse_city_seq()%>" />
	    		<!-- 입력양식 -->
	    		<div class="form-group">
	    			<label for="name" class="col-md-2 control-label">민박 이름</label>
	    			<div class="col-md-10">
	    				<input type="text" class="form-control" id="name" placeholder="민박 이름을 입력하세요." name="name" value="<%=item.getName()%>">
	    			</div>
	    		</div>
	    		<!-- // 입력양식 -->
	    		<!-- 입력양식 -->
	    		<div class="form-group">
	    			<label for="city_name" class="col-md-2 control-label">도시 분류</label>
	    			<div class="col-md-10">
	    				<select name="city_name" id="city_name" class="form-control">
	    					<option> -- 선택하세요 --</option>
	    					<option value="1" <%if(item.getHouse_city_seq() == 1) {%> selected <%}%>>베네치아</option>
	    					<option value="2" <%if(item.getHouse_city_seq() == 2) {%> selected <%}%>>로마</option>
	    				</select>
	    			</div>
	    		</div>
	    		<!-- // 입력양식 -->
	    		<!-- 입력양식 -->
	    		<div class="form-group">
	    			<label for="address1" class="col-md-2 control-label">주소 : 국가</label>
	    			<div class="col-md-10">
	    				<select name="address1" id="address1" class="form-control">
	    					<option> ---- 선택하세요 ---- </option>
	    					<option value="이탈리아" <%if(item.getAddress1().equals("이탈리아")) {%> selected <%}%>> 이탈리아 </option>
	    				</select>
	    			</div>
	    		</div>
	    		<!-- // 입력양식 -->
	    		<div class="form-group">
	    			<label for="address2" class="col-md-2 control-label">주소 : 도시</label>
	    			<div class="col-md-10">
	    				<select name="address2" id="address2" class="form-control">
	    					<option> ---- 선택하세요 ---- </option>
	    					<option value="베네치아" <%if (city.equals("1") || item.getAddress2().equals("베네치아")) {%> selected <%}%>> 베네치아 </option>
	    					<option value="로마" <%if (city.equals("2") || item.getAddress2().equals("로마")) {%> selected <%}%>> 로마 </option>
	    				</select>
	    			</div>
	    		</div>
	    		<!-- 입력양식 -->
	    		<div class="form-group">
	    			<label for="address3" class="col-md-2 control-label">주소 : 상세주소</label>
	    			<div class="col-md-10">
	    				<input type="text" class="form-control" id="address3" placeholder="상세주소를 입력하세요." name="address3" value="<%=item.getAddress3()%>">
	    			</div>
	    		</div>
	    		<!-- 입력양식 -->
	    		<div class="form-group">
	    			<label for="address4" class="col-md-2 control-label">주소 : 기타</label>
	    			<div class="col-md-10">
	    				<input type="text" class="form-control" id="address4" placeholder="주변 명소에서 찾아오는 법을 적어주세요." name="address4" value="<%=item.getAddress4()%>">
	    			</div>
	    		</div>
	    		<!-- 입력양식 -->
	    		<div class="form-group">
	    			<label for="email" class="col-md-2 control-label">이메일</label>
	    			<div class="col-md-10">
	    				<input type="text" class="form-control" id="email" placeholder="이메일을 입력하세요." name="email" value="<%=item.getEmail()%>">
	    			</div>
	    		</div>
	    		<!-- 입력양식 -->
	    		<div class="form-group">
	    			<label for="homepage" class="col-md-2 control-label">홈페이지</label>
	    			<div class="col-md-10">
	    				<input type="text" class="form-control" id="homepage" placeholder="홈페이지를 입력하세요." name="homepage" value="<%=item.getHomepage()%>">
	    			</div>
	    		</div>
	    		<!-- 입력양식 -->
	    		<div class="form-group">
	    			<label for="info" class="col-md-2 control-label">정보</label>
	    			<div class="col-md-10">
	    				<textarea id="info" name="info" class="form-control" rows="5" placeholder="민박의 정보를 입력하세요."><%=item.getInfo().replace("<br />", "\n")%></textarea>
	    			</div>
	    		</div>
	    		<!-- 입력양식 -->
	    		<div class="form-group">
	    			<label for="tel[]" class="col-md-2 control-label">연락처</label>
	    			<div class="col-md-10">
	    				<p class="form-control-static"><a href="#" id="frmTelAdd">추가하기</a></p>
<%
	if(t_item != null) {
		for(int i=0; i<t_item.size(); i++) {
			TelItem item2 = t_item.get(i);
%>
					<div class="input-group tel_group">
						<input type="tel" class="form-control input_item" id="tel[]" placeholder="연락처를 입력하세요." name="tel[]" value="<%=item2.getTel()%>">
						<span class="input-group-addon"><button type="button" class="input-group tel_delete" value="x">x</button></span>
					</div>
<%
		}
	}
%>
	    				<div id="tel_add">
	    				</div>
	    			</div>
	    		</div>
	    		<!-- // 입력양식 -->
	    		<!-- 입력양식 -->
	    		<div class="form-group">
	    			<label for="image[]" class="col-md-2 control-label">이미지</label>
	    			<div class="col-md-10">
	    				<p class="form-control-static"><a href="#" id="frmImageAdd">추가하기</a></p>
<%
	if(i_item != null) {
		for(int i=0; i<i_item.size(); i++) {
			ImageItem item3 = i_item.get(i);
%>
						<div class="input-group image_group">
							<input type="text" class="form-control" id="image[]" placeholder="이미지 경로를 입력하세요." name="image[]" value="<%=item3.getImage_url()%>">
							<span class="input-group-addon"><button type="button" class="input-group image_delete" value="x">x</button></span>
					</div>
<%
		}
	}
%>
	    				<div id="image_add">
	    				</div>
	    			</div>
	    		</div>
	    		<!-- // 입력양식 -->
	    		<!-- 입력양식 -->
	    		<div class="form-group">
	    			<label for="room[]" class="col-md-2 control-label">룸정보</label>
	    			<div class="col-md-10">
	    				<p class="form-control-static"><a href="#" id="frmRoomAdd">추가하기</a></p>
<%
	if (r_item != null) {
		for (int i=0; i<r_item.size(); i++) {
			RoomItem item4 = r_item.get(i);
%>
						<div class="form-group div_item">
	    					<div class="col-md-3 input-group div1_item">
	    						<span class="input-group-addon">룸이름</span>
	    						<input type="text" class="form-control" name="room_name[]" value="<%=item4.getName()%>"/>
	    					</div>
	    					<div class="col-md-2 input-group div2_item">
	    						<span class="input-group-addon">최대인원</span>
	    						<input type="text" class="form-control" name="room_limitPerson[]" value="<%=item4.getLimit_person()%>"/>
	    					</div>
	    					<div class="col-md-3 input-group div3_item">
	    						<span class="input-group-addon">비수기가격</span>
	    						<input type="text" class="form-control" name="room_offPrice[]" value="<%=item4.getOff_price()%>"/>
	    					</div>
	    					<div class="col-md-3 input-group div4_item">
	    						<span class="input-group-addon">성수기가격</span>
	    						<input type="text" class="form-control" name="room_onPrice[]" value="<%=item4.getOn_price()%>"/>
	    					</div>
	    					<div class="col-md-1 div5_item">
	    						<input type="button" class="close room_delete" value="x" />
	    					</div>
	    				</div>
<%
		}
	}
%>
	    				<div id="room_add">
	    				</div>
	    			</div>
	    		</div>
	    		<!-- // 입력양식 -->
	    		<div class="form-group">
	    			<div class="col-md-offset-2 col-md-10">
	    				<div class="row">
	    					<div class="col-md-4">
	    						<button type="submit" class="btn btn-primary btn-block">저장하기</button>
	    					</div>
	    					<div class="col-md-4">
	    						<button type="reset" class="btn btn-info btn-block">다시작성</button>
	    					</div>
	    					<div class="col-md-4">
	    						<a href="listHouse.jsp?city=<%=item.getHouse_city_seq()%>" class="btn btn-warning btn-block">목록보기</a>
	    					</div>
	    				</div>
	    			</div>
	    		</div>
	    		</fieldset>
	    	</form>
    </div>
	<script type="text/javascript">
    	$(function() {
    		$("#frmHouseEdit").submit(function() {
    			// 민박 이름 입력 검사
    			if (!$("input[name='name']").val().value()) {
    				alert("민박 이름을 입력하세요.");
    				$("input[name='name']").focus();
    				return false;
    			}
    			// 국가 선택 검사
    			if ($("select[name='address1'] > option:selected").index() < 1) {
    				alert("국가를 선택하세요.");
    				$("select[name='address1']").focus();
    				return false;
    			}
    			// 도시 선택 검사
    			if ($("select[name='address2'] > option:selected").index() < 1) {
    				alert("도시를 선택하세요.");
    				$("select[name='address2']").focus();
    				return false;
    			}
    			// 상세 주소 입력 검사
    			if (!$("input[name='address3']").val().value()) {
    				alert("상세 주소를 입력하세요.");
    				$("input[name='address3']").focus();
    				return false;
    			}
    			// 기타 주소 입력 검사
    			if (!$("input[name='address4']").val().value()) {
    				alert("기타 주소를 입력하세요.");
    				$("input[name='address4']").focus();
    				return false;
    			}
    			// 이메일 입력 검사
    			if (!$("input[name='email']").val().value()) {
    				alert("이메일을 입력하세요.");
    				$("input[name='email']").focus();
    				return false;
    			}
    			// 홈페이지 입력 검사
    			if (!$("input[name='homepage']").val().value()) {
    				alert("홈페이지를 입력하세요.");
    				$("input[name='homepage']").focus();
    				return false;
    			}
    			// 민박 정보 입력 검사
    			if (!$("textarea[name='info']").val().value()) {
    				alert("민박 정보를 입력하세요.");
    				$("textarea[name='info']").focus();
    				return false;
    			}
    			// 연락처 입력 검사
    			var tel = true;
    			$("input[name='tel[]']").each(function(idx, item) {
    				if(!$(item).val().isValue()) {
    					alert("연락처를 입력하세요.");
    					$(item).focus();
    					tel = false;
    				}
    			});
    			if(!tel) {
    				return false;
    			}
    			
    			// 이미지 입력 검사
    			var image = true;
    			$("input[name='image[]']").each(function(idx, item) {
    				if (!$(item).val().value()) {
    					alert("이미지를 추가하세요.");
    					$(item).focus();
    					image = false;
    				}
    			});
    			if(!image) {
    				return false;
    			}
    			
    			// 룸이름 입력 검사
    			if (!$("input[name='room_name[]']").val().value()) {
    				alert("룸 이름을 입력하세요.");
    				$("input[name='room_name[]']").focus();
    				return false;
    			}
    			// 룸 최대인원 입력 검사
    			if (!$("input[name='room_limitPerson[]']").val().value()) {
    				alert("룸 최대인원을 입력하세요.");
    				$("input[name='room_limitPerson[]']").focus();
    				return false;
    			}
    			// 룸 비수기가격 입력 검사
    			if (!$("input[name='room_offPrice[]']").val().value()) {
    				alert("룸 비수기 가격을 입력하세요.");
    				$("input[name='room_offPrice[]']").focus();
    				return false;
    			}
    			// 룸 성수기가격 입력 검사
    			if (!$("input[name='room_onPrice[]']").val().value()) {
    				alert("룸 성수기 가격을 입력하세요.");
    				$("input[name='room_onPrice[]']").focus();
    				return false;
    			}
    		});
    		
    		// 연락처 추가 처리
    		$("#frmTelAdd").click(function() {
				var new_div = $("<div>");
				new_div.addClass("input-group div_item");
				var new_input = $("<input>");
				new_input.addClass("input_item form-control");
				new_input.attr({
					'type' : 'tel', 'placeholder' : '연락처를 입력하세요', 'name' : 'tel[]'
				});
				var new_span = $("<span>");
				new_span.addClass("span_item input-group-addon");
				var new_button = $("<button>");
				new_button.attr("type=button");
				new_button.html('&times;');
				new_button.addClass("delete_btn close");
				
				new_button.click(function() {
			    	if (confirm("정말 선택하신 항목을 삭제하시겠습니까?")) {
			    		$(this).parents(".div_item").remove();
			    	}
			    });
				new_span.append(new_button);
				new_div.append(new_input).append(new_span);
				$("#tel_add").append(new_div);
				return false;
			});
    		
    		$(".tel_delete").click(function() {
    			if (confirm("정말 선택하신 항목을 삭제하시겠습니까?")) {
					$(this).parents(".tel_group").remove();
				}
    		});
    		
    		// 이미지 추가 처리
    		$("#frmImageAdd").click(function() {
    			var new_div = $("<div>");
    			new_div.addClass("input-group div_item");
    			var new_input = $("<input>");
    			new_input.addClass("input_item form-control");
    			new_input.attr({
    				'type' : 'text' , 'placeholder' : '이미지를 추가하세요.' , 'name' : 'image[]'
    			});
    			var new_span = $("<span>");
    			new_span.addClass("span_item input-group-addon");
    			var new_button = $("<button>");
    			new_button.attr("type=button");
    			new_button.html('&times;');
    			new_button.addClass("delete_btn close");
    			
    			new_button.click(function() {
    				if (confirm("정말 선택하신 항목을 삭제하시겠습니까?")) {
			    		$(this).parents(".div_item").remove();
			    	}
    			});
    			new_span.append(new_button);
    			new_div.append(new_input).append(new_span);
    			$("#image_add").append(new_div);
    			return false;
    		});
    		
    		$(".image_delete").click(function() {
    			if (confirm("정말 선택하신 항목을 삭제하시겠습니까?")) {
					$(this).parents(".image_group").remove();
				}
    		});
    		
    		// 룸 정보 추가 처리
    		$("#frmRoomAdd").click(function() {
    			var new_div = $("<div>");
    			new_div.addClass("form-group div_item");
    			
    			var new_div1 = $("<div>");
    			new_div1.addClass("col-md-3 input-group div1_item");
    			var new_span1 = $("<span>");
    			new_span1.addClass("input-group-addon span1_item");
    			new_span1.html("룸이름");
    			var new_input1 = $("<input>");
    			new_input1.addClass("form-control input1_item");
    			new_input1.attr({
    				'type' : 'text', 'name' : 'room_name[]'
    			});
    			
    			var new_div2 = $("<div>");
    			new_div2.addClass("col-md-2 input-group div2_item");
    			var new_span2 = $("<span>");
    			new_span2.addClass("input-group-addon span2_item");
    			new_span2.html("최대인원");
    			var new_input2 = $("<input>");
    			new_input2.addClass("form-control input2_item");
    			new_input2.attr({
    				'type' : 'text', 'name' : 'room_limitPerson[]'
    			});
    			
    			var new_div3 = $("<div>");
    			new_div3.addClass("col-md-3 input-group div3_item");
    			var new_span3 = $("<span>");
    			new_span3.addClass("input-group-addon span3_item");
    			new_span3.html("비수기가격");
    			var new_input3 = $("<input>");
    			new_input3.addClass("form-control input3_item");
    			new_input3.attr({
    				'type' : 'text', 'name' : 'room_offPrice[]'
    			});
    			
    			var new_div4 = $("<div>");
    			new_div4.addClass("col-md-3 input-group div4_item");
    			var new_span4 = $("<span>");
    			new_span4.addClass("input-group-addon span4_item");
    			new_span4.html("성수기가격");
    			var new_input4 = $("<input>");
    			new_input4.addClass("form-control input4_item");
    			new_input4.attr({
    				'type' : 'text', 'name' : 'room_onPrice[]'
    			});
    			
    			var new_div5 = $("<div>");
    			new_div5.addClass("col-md-1 div5_item");
    			var new_input5 = $("<input>");
    			new_input5.addClass("close");
    			new_input5.attr({
    				'type' : 'button' , 'value' : 'x'
    			});
    			
    			new_input5.click(function() {
    				if (confirm("정말 선택하신 항목을 삭제하시겠습니까?")) {
    					$(this).parents(".div_item").remove();
    				}
    			});
    			
    			new_div1.append(new_span1).append(new_input1);
    			new_div2.append(new_span2).append(new_input2);
    			new_div3.append(new_span3).append(new_input3);
    			new_div4.append(new_span4).append(new_input4);
    			new_div5.append(new_input5);
    			new_div.append(new_div1).append(new_div2).append(new_div3).append(new_div4).append(new_div5);
    			$("#room_add").append(new_div);
    			return false;
    		});
    		
			$(".room_delete").click(function() {
    			if (confirm("정말 선택하신 항목을 삭제하시겠습니까?")) {
					$(this).parents(".div_item").remove();
				}
    		});
    	});
    </script>
  </body>
</html>